﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MKAdminCore.Controllers;
using MKAdminCore.DTO.CusAttribute;
using MKAdminCore.DTO.Web.BasicSetting;
using MKAdminCore.DTO.Web.Common;
using MKAdminCore.IService.Web.BasicSetting;
using MKAdminCore.ToolKit.Config;

namespace MKAdminCore.Areas.BasicSetting.Controllers
{
    [Area("BasicSetting")]
    [Route("BasicSetting/[controller]/[action]")]
    public class UserInfoController : BaseMvcController
    {
        private IUserInfoService _iUserInfoService;

        public UserInfoController(IUserInfoService iUserInfoService)
        {
            _iUserInfoService = iUserInfoService;
        }

        public IActionResult Index()
        {
            return View(PageViewFilesConfig.BasicSettingUserInfo);
        }

        [HttpPost]
        public IActionResult List(GetUserInfoListParameter parameter)
        {
            var result = _iUserInfoService.List(parameter);

            return Ok(result);
        }

        [HttpPost]
        public async Task<IActionResult> Add(UserInfoAdd parameter)
        {
            var result = new HttpResponseResult();
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }

            var userInfo = base.GetUserInfo();
            result = await _iUserInfoService.Add(parameter, userInfo);

            return Ok(result);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(UserInfoEdit parameter)
        {
            var result = new HttpResponseResult();
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }

            result = await _iUserInfoService.Edit(parameter);

            return Ok(result);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ResetPwd(UserInfoResetPassword parameter)
        {
            var result = new HttpResponseResult();
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }

            result = await _iUserInfoService.ResetPwd(parameter);

            return Ok(result);

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Del(UserInfoDel parameter)
        {
            var result = new HttpResponseResult();
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }

            result = await _iUserInfoService.Del(parameter);

            return Ok(result);

        }

        /// <summary>
        /// 启用/禁用
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateStatus(UserInfoUpdateStatus parameter)
        {
            var result = new HttpResponseResult();
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }

            result = await _iUserInfoService.UpdateStatus(parameter);

            return Ok(result);
        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetRoleInfo(UserInfoGetRoleInfo parameter)
        {
            var result = new HttpResponseResult();
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }

            result = await _iUserInfoService.GetRoleInfo(parameter);

            return Ok(result);
        }

        /// <summary>
        /// 分配角色
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AllotRole(AllotRoleInfo parameter)
        {
            var result = new HttpResponseResult();
            var validateResult = parameter.Validate();
            if (validateResult.Code != 200)
            {
                result.Msg = validateResult.Msg;
                result.Code = validateResult.Code;
                return Ok(result);
            }

            result = await _iUserInfoService.AllotRole(parameter);

            return Ok(result);
        }
    }
}